Thread: [C] Need help to export data to excel

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    [C] Need help to export data to excel

    1. Code:
    2. #include <stdio.h>
    3. typedef struct {
    4. char nombre[30];
    5. int codigo;
    6. float costo;
    7. float precio;
    8. int cantidad;
    9. } producto;
    10. void entrarregistro(producto registro);
    11. void modificarcodigo(producto registro);
    12. void modificarnombre(producto registro);
    13. void modificarcosto(producto registro);
    14. void modificarprecio(producto registro);
    15. void modificarcantidad(producto registro);
    16. void buscarproducto(producto registro);
    17. int main(int argc, const char * argv[])
    18. {
    19. producto(registro);
    20. int centinela;
    21. do {
    22. printf("Menu:\n");
    23. printf("0.- Salir del programa\n");
    24. printf("1.- Entrar nuevo registro\n");
    25. printf("2.- Modificar codigo de producto\n");
    26. printf("3.- Modificar nombre de producto\n");
    27. printf("4.- Modificar costo de producto\n");
    28. printf("5.- Modificar precio de producto\n");
    29. printf("6.- Modificar cantidad de producto\n");
    30. printf("7.- Buscar un producto\n");
    31. printf("Seleccione una opcion:\n");
    32. scanf("%d", &centinela);
    33. switch(centinela){
    34. case 0:
    35. break;
    36. case 1:
    37. entrarregistro(registro);
    38. break;
    39. case 2:
    40. modificarcodigo(registro);
    41. break;
    42. case 3:
    43. modificarnombre(registro);
    44. break;
    45. case 4:
    46. modificarcosto(registro);
    47. break;
    48. case 5:
    49. modificarprecio(registro);
    50. break;
    51. case 6:
    52. modificarcantidad(registro);
    53. break;
    54. case 7:
    55. buscarproducto(registro);
    56. break;
    57. }
    58. } while (centinela != 0);
    59. return 0;
    60. }
    61. void entrarregistro(producto registro)
    62. {
    63. FILE * fd;
    64. fd = fopen("/Users/David/Desktop/datos.dat", "a");
    65. if(fd == NULL){
    66. printf("Error al abrir el archivo");
    67. exit(1);
    68. }
    69. printf("Entre el nombre del producto:\n");
    70. scanf("%s", registro.nombre);
    71. printf("Entre el codigo del producto:\n");
    72. scanf("%d", &registro.codigo);
    73. printf("Entre el costo del producto:\n");
    74. scanf("%f", &registro.costo);
    75. printf("Entre el precio del producto:\n");
    76. scanf("%f", &registro.precio);
    77. printf("Entre la cantidad:\n");
    78. scanf("%d", &registro.cantidad);
    79. fprintf(fd, "%s %d %7.2f %7.2f %d\n", registro.nombre, registro.codigo, registro.costo, registro.precio,registro.cantidad);
    80. fclose(fd);
    81. }
    82. void modificarcodigo(producto registro)
    83. {
    84. FILE * fd;
    85. int c = 0;
    86. fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
    87. if(fd == NULL){
    88. printf("Error al abrir el archivo");
    89. exit(1);
    90. }
    91. printf("Seleccina el codigo del producto a modificar: \n");
    92. fread(&registro, sizeof(producto), 1, fd);
    93. scanf("%d", &c);
    94. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    95. fread(&registro, sizeof(producto), 1, fd);
    96. printf("Entre el nuevo codigo:\n");
    97. scanf("%d", &registro.codigo);
    98. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    99. fwrite(&registro, sizeof(producto), 1, fd);
    100. fclose(fd);
    101. }
    102. void modificarnombre(producto registro)
    103. {
    104. FILE * fd;
    105. int c = 0;
    106. fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
    107. if(fd == NULL){
    108. printf("Error al abrir el archivo");
    109. exit(1);
    110. }
    111. printf("Seleccina el codigo del producto a modificar: \n");
    112. fread(&registro, sizeof(producto), 1, fd);
    113. while(!feof(fd)){
    114. fread(&registro, sizeof(producto), 1, fd);
    115. }
    116. printf("?:\n");
    117. scanf("%d", &c);
    118. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    119. fread(&registro, sizeof(producto), 1, fd);
    120. printf("Entre el nuevo nombre : \n");
    121. scanf("%s", registro.nombre);
    122. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    123. fwrite(&registro, sizeof(producto), 1, fd);
    124. fclose(fd);
    125. }
    126. void modificarcosto(producto registro)
    127. {
    128. FILE * fd;
    129. int c = 0;
    130. fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
    131. if(fd == NULL){
    132. printf("Error al abrir el archivo");
    133. exit(1);
    134. }
    135. printf("Seleccina el codigo del producto a modificar: \n");
    136. fread(&registro, sizeof(producto), 1, fd);
    137. while(!feof(fd)){
    138. fread(&registro, sizeof(producto), 1, fd);
    139. }
    140. printf("?:\n");
    141. scanf("%d", &c);
    142. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    143. fread(&registro, sizeof(producto), 1, fd);
    144. printf("Entre el nuevo costo: \n");
    145. scanf("%f", &registro.costo);
    146. if(registro.costo < 0){
    147. printf("No se puede tener un costo por debajo de 0");
    148. exit(1);
    149. }
    150. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    151. fwrite(&registro, sizeof(producto), 1, fd);
    152. fclose(fd);
    153. }
    154. void modificarprecio(producto registro)
    155. {
    156. FILE * fd;
    157. int c = 0;
    158. fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
    159. if(fd == NULL){
    160. printf("Error al abrir el archivo");
    161. exit(1);
    162. }
    163. printf("Seleccina el codigo del producto a modificar: \n");
    164. fread(&registro, sizeof(producto), 1, fd);
    165. while(!feof(fd)){
    166. fread(&registro, sizeof(producto), 1, fd);
    167. }
    168. printf("?:\n");
    169. scanf("%d", &c);
    170. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    171. fread(&registro, sizeof(producto), 1, fd);
    172. printf("Entre el nuevo numero precio: \n");
    173. scanf("%f", &registro.precio);
    174. if(registro.precio < 0){
    175. printf("No se puede tener un precio por debajo de 0");
    176. exit(1);
    177. }
    178. else{
    179. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    180. fwrite(&registro, sizeof(producto), 1, fd);
    181. fclose(fd);
    182. }
    183. }
    184. void modificarcantidad(producto registro)
    185. {
    186. FILE * fd;
    187. int c = 0;
    188. fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
    189. if(fd == NULL){
    190. printf("Error al abrir el archivo");
    191. exit(1);
    192. }
    193. printf("Seleccina el codigo del producto a modificar: \n");
    194. fread(&registro, sizeof(producto), 1, fd);
    195. while(!feof(fd)){
    196. fread(&registro, sizeof(producto), 1, fd);
    197. }
    198. printf("?:\n");
    199. scanf("%d", &c);
    200. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    201. fread(&registro, sizeof(producto), 1, fd);
    202. printf("Entre la cantidad nueva: \n");
    203. scanf("%d", &registro.cantidad);
    204. if(registro.cantidad < 0){
    205. printf("No se pueden tener cantidades negativas\n");
    206. exit(1);
    207. }
    208. else{
    209. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    210. fwrite(&registro, sizeof(producto), 1, fd);
    211. fclose(fd);
    212. }
    213. }
    214. void buscarproducto(producto registro)
    215. {
    216. FILE * fd;
    217. int c;
    218. fd = fopen("/Users/David/Desktop/datos.dat", "r");
    219. if (fd == NULL){
    220. printf("Error al abrir el archivo");
    221. exit(1);
    222. }
    223. printf("Entre el codigo del producto a buscar:\n");
    224. fread(&registro, sizeof(producto), 1, fd);
    225. while (!feof(fd)){
    226. fread(&registro, sizeof(producto), 1, fd);
    227. }
    228. scanf("%d", &c);
    229. fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
    230. fread(&registro, sizeof(producto), 1, fd);
    231. printf("%s\t%d\t%f\t%f\t%d\n", registro.nombre, registro.codigo, registro.costo, registro.precio,registro.cantidad);
    232. fclose(fd);
    233. }


  2. So there is my whole code, it's a program to manage an inventory. The first function is to create a new entry, the last one is to search for a product using the code and the rest are to modify data. Anyway, now I need to know how to export all the data to a spreadsheet. If I open the file the program creates with excel it actually shows the name but after that it shows random symbols. Thanks for the help!

  • #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Please learn to use code tags.

    Storing your data in a binary file is not a good idea if other programs should be able to use it.

    The easiest way is to store it in plain text, e.g. as comma separated values.

    Bye, Andreas

  • #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Need help to export data to excel
    No you dont, you just need help to write output to a flatfile, you also need help writing code that is readable, indent, sort out your FunctionNames(..) - por ejemplo:

    modificarcantidad() vs ModificarCantidad()

    ¿ves la diferencía?

    and consider using other souce files.
    Last edited by rogster001; 05-06-2013 at 03:53 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  • Popular pages Recent additions subscribe to a feed

    Similar Threads

    1. Replies: 5
      Last Post: 05-31-2012, 07:04 AM
    2. Sending Data to Excel with C++
      By chopshardiman in forum C++ Programming
      Replies: 9
      Last Post: 05-17-2012, 11:24 AM
    3. Data Organization and Excel
      By uniqst3r in forum C Programming
      Replies: 4
      Last Post: 05-26-2008, 01:37 AM
    4. Datagrid Export to MS Excel with C++ .NET
      By Gerudom in forum Windows Programming
      Replies: 3
      Last Post: 06-09-2006, 07:58 AM